﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.BreedCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.BreedCommandHandler
{
    public class AddBreedCommandHandler : IRequestHandler<AddBreedCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_BreedInfo> breedRep;
        private readonly IMapper mapper;

        public AddBreedCommandHandler(IBaseRepository<SU_BreedInfo> breedRep, IMapper mapper)
        {
            this.breedRep = breedRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(AddBreedCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model=mapper.Map<SU_BreedInfo>(request);
            model.ProductionCode = request.ProductionCode;
            var res=breedRep.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }
    }
}
